package com.meila.cms.controller;

import java.util.List;

import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.meila.cms.auth.AuthClass;
import com.meila.cms.entity.AjaxObj;
import com.meila.cms.model.CommonModel;
import com.meila.cms.model.DictModel;
import com.meila.cms.model.PropertyUniqueModel;
import com.meila.cms.model.SQLAdapterModel;
import com.meila.cms.model.UserModel;
import com.meila.cms.util.HttpUtils;
import com.meila.cms.util.config.Property;
import com.meila.cms.util.json.JsonUtil1;
import com.meila.cms.util.json.JsonUtil2;
import com.meila.cms.util.json.JsonUtil3;

@Controller
@RequestMapping("/common")
@AuthClass("login")
public class CommonController extends BaseController {

	/**
	 * 参数 category,isquery
	 */
	@RequestMapping("/selectByComboCategory")
	@ResponseBody
	public List selectByComboCategory(DictModel dictModel) {

		List<CommonModel> list = null;
		try {

			SQLAdapterModel sqlAdapterModel = new SQLAdapterModel(
					"select distinct type from t_dict where category ='"
							+ dictModel.getCategory() + "'");
			int type = dictMapper.selectCount(sqlAdapterModel);

			if (type == 1) {

				// 查询字典数据字典配置的sql
				sqlAdapterModel
						.setSql("select sqls from t_dict where category ='"
								+ dictModel.getCategory() + "'");
				String sqls =  dictMapper.selectString(sqlAdapterModel);
				if("City".equals(dictModel.getCategory())){
					sqls=sqls.replace("${where}"," ParentId="+dictModel.getIdStr());
				}
				sqlAdapterModel.setSql(sqls);
				list =  dictMapper.selectBySql(sqlAdapterModel);

			} else if (type == 0) {

				// 查询字典数据字典配置的是常量
				sqlAdapterModel
						.setSql("select name as text,value from t_dict where category ='"
								+ dictModel.getCategory() + "'");
				list =  dictMapper.selectBySql(sqlAdapterModel);

			}

		} catch (Exception ex) {

			ex.printStackTrace();
			logger.error("出错:", ex);
		}
		return list;

	}
	
	
	/**
	 * 根据传入的属性名和值判断是否和已有的数据重复
	 * @param uniqueObj
	 * @return
	 */
	@RequestMapping("/checkNameUnique")
	@ResponseBody
	public AjaxObj checkNameUnique(PropertyUniqueModel propertyUniqueModel) {
		
		AjaxObj ajaxObj=new AjaxObj();
		ajaxObj.setResult(false);
		
		
		PropertyUniqueModel propertyUniqueModel2 =	propertyUniqueMapper.selectByCategory(propertyUniqueModel.getCategory());
		propertyUniqueModel2.setPropertyvalue(propertyUniqueModel.getPropertyvalue());
		propertyUniqueModel2.setId(propertyUniqueModel.getId());
		
		SQLAdapterModel sqlAdapterModel=new SQLAdapterModel();
		String sql=" select count(1) " +
				   " from "+propertyUniqueModel2.getTablename()+
				   " where "+propertyUniqueModel2.getPropertyname()+" ='"+propertyUniqueModel2.getPropertyvalue()+"'";
		
		if(!StringUtils.isEmpty(propertyUniqueModel.getId())){
			sql=sql+" and "+propertyUniqueModel2.getIdname()+"!="+propertyUniqueModel2.getId();
		}
		if(!StringUtils.isEmpty(propertyUniqueModel.getWhere())){
			sql=sql+"  "+propertyUniqueModel.getWhere();
		}
		
		logger.info(sql);
		sqlAdapterModel.setSql(sql);
		int count= dictMapper.selectCount(sqlAdapterModel);
		if(count>0){
			ajaxObj.setResult(true);
			ajaxObj.setMsg(propertyUniqueModel2.getPropertyvalue()+"已经存在");
		}
		
		return ajaxObj;
	}
	


}
